﻿using DayuCode.Designer.Common;
using DayuCode.Designer.Entity;
using DayuCode.Designer.Service;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DayuCode.Designer.Forms.template
{
    public partial class FormCodeFragments : UIForm
    {
        public FormCodeFragments()
        {
            InitializeComponent();

            dgvCodeFragments.AutoGenerateColumns = false;

            txtFragCode.FillColor = Color.FromArgb(235, 243, 255);
            loadCodeFragments(null);
        }

        private void loadCodeFragments(long? selectedId)
        {
            var fragments = CodeFragmentHandler.getAllCodeFragments();
            dgvCodeFragments.DataSource = fragments;
            if (selectedId != null)
            {
                int idx = fragments.FindIndex(item => item.Id == selectedId);
                dgvCodeFragments.SelectedIndex = idx;
            }
            else
            {
                if (fragments.Count > 0)
                {
                    dgvCodeFragments.SelectedIndex = 0;
                }
            }
        }

        private void btnAddCodeFragment_Click(object sender, EventArgs e)
        {
            FormCodeFragment form = new FormCodeFragment(null);
            if(form.ShowDialog() == DialogResult.OK)
            {
                CodeFragment codeFragment = form.CodeFragment;
                loadCodeFragments(codeFragment.Id);
            }
            form.Dispose();

        }

        private void btnEditCodeFragment_Click(object sender, EventArgs e)
        {
            if (dgvCodeFragments.SelectedRows.Count == 0) return;
            CodeFragment codeFragment = dgvCodeFragments.SelectedRows[0].DataBoundItem as CodeFragment;
            FormCodeFragment form = new FormCodeFragment(codeFragment.Id);
            if (form.ShowDialog() == DialogResult.OK)
            {                
                loadCodeFragments(codeFragment.Id);
            }
            form.Dispose();
        }

        private void btnDelCodeFragment_Click(object sender, EventArgs e)
        {
            if (dgvCodeFragments.SelectedRows.Count == 0) return;
            CodeFragment codeFragment = dgvCodeFragments.SelectedRows[0].DataBoundItem as CodeFragment;
            if(this.ShowAskDialog("确认删除代码片段【"+ codeFragment .Name+ "】吗？"))
            {
                CodeFragmentHandler.deleteCodeFragment((long)codeFragment.Id);
                this.ShowSuccessTip("完成删除！");
                loadCodeFragments(null);
            }
        }

        private void dgvCodeFragments_SelectIndexChange(object sender, int index)
        {
            if (dgvCodeFragments.SelectedRows.Count == 0) return;
            CodeFragment codeFragment = dgvCodeFragments.SelectedRows[0].DataBoundItem as CodeFragment;
            txtFragCode.Text = codeFragment.Code;

            txtCodeTemplateText.ResetText();
            txtCodeTemplateText.Invalidate();
            txtCodeTemplateText.Refresh();
            txtCodeTemplateText.Text = ""+codeFragment.CodeTemplate;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (dgvCodeFragments.SelectedRows.Count == 0) return;
            CodeFragment codeFragment = dgvCodeFragments.SelectedRows[0].DataBoundItem as CodeFragment;

            string prix = string.Format("<#--{0}-->", codeFragment.Code);
            string ftltext = txtCodeTemplateText.Text;
            if (!ftltext.StartsWith(prix))
            {
                this.ShowErrorTip("当前代码可能不属于此文件模板，第一行不是模板编号:\n" + prix + "\r\n已取消保存。", 3000);
                return;
            }

            CodeFragmentHandler.updateCodeTemplateText((long)codeFragment.Id, ftltext);
            codeFragment.CodeTemplate = txtCodeTemplateText.Text;

            this.ShowSuccessTip("保存成功！");
        }
    }
}
